<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IBillAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];

  	class MySqlBillAccessDao implements IBillAccessDao {
  		/**
		 * Constructor of the bill dao
		 */
	 	public function __construct () {}
		
		/**
		 * Creates a bill
		 */
     	public function createBill($bill) {
			$pkNumber = $bill -> getPkNumber();
			$pkControlNumber = $bill -> getPkControlNumber();
			$date = $bill -> getDate();
			$tax = $bill -> getTax();
			$idClient = $bill -> getIdClient();
			$ciRif = $bill -> getCiRif();
			
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			if (strcmp($ciRif, 'ci') == 0)
				$query = "INSERT INTO BILL (pk_number, pk_control_number, date, tax, 
					fk_id_number_natural_customer) VALUES ($pkNumber, $pkControlNumber, '$date', 
					$tax, $idClient)";
			else
				$query = "INSERT INTO BILL (pk_number, pk_control_number, date, tax, 
					fk_rif_legal_client) VALUES ($pkNumber, $pkControlNumber, '$date', 
					$tax, '$idClient')";
					
			$sentence = mysql_query($query, $conn);
			
			if ($sentence)
				return true;
			
			return false;
	 	}
	 
	 	public function changeBill  ($pk_number, $pk_control_number, $date, $tax, 
			$fk_id_number_natural_customer, $fk_rif_legal_client) {
				
			$connection = ConnectionFactory::getMySqlConnectionAccessDao();
			$conn=$connection->openConnection();
			$conn = mysql_connect("localhost", "root", "");
			mysql_select_db("sager", $conn);
			
			$query = "UPDATE BILL SET $pk_number=$pk_number, $pk_control_number=$pk_control_number, $date='$date', $tax='$tax', 
				$fk_id_number_natural_customer='$fk_id_number_natural_customer', $fk_rif_legal_client='$fk_rif_legal_client'";
		 
		 
		 
			$sentence=mysql_query($query,$conn);
			   if ($sentence){
					return true;
				   }
			  return false;
	 
		 }
		 		 	 
	  /**
		 * Selects all Bills
		 */
		public function selectBills() {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			
					$query = "SELECT `pk_number`,`pk_control_number`,`date`,pk_rif,name,email_address
							  FROM BILL, LEGAL_CLIENT
							  WHERE `fk_rif_legal_client`=pk_rif";
				
			
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
				$GLOBALS['debug']->log($query,'Funcion DAO');
			return $sentence;
		 }
		 
		 /**
		 * Selects all Bills Ductobarra
		 */
		public function selectBillsDuctobarra() {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			
					$query = "SELECT `pk_number`,`pk_control_number`,`date`,pk_id_number,first_name,first_last_name,email_address
							  FROM BILL, NATURAL_CUSTOMER
							  WHERE `fk_id_number_natural_customer`=pk_id_number";
				
			
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
				$GLOBALS['debug']->log($query,'Funcion DAO');
			return $sentence;
		 }
		 
		 

		 
  
  }


?>